package ru.vsu.cs.pm.pages;

import org.apache.wicket.markup.html.form.*;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import ru.vsu.cs.pm.BasePage;
import ru.vsu.cs.pm.PMSession;
import ru.vsu.cs.pm.beans.AccountManager;
import ru.vsu.cs.pm.beans.DealerShipManager;
import ru.vsu.cs.pm.beans.ItemManager;
import ru.vsu.cs.pm.exceptions.IllegalAmountOfMoney;
import ru.vsu.cs.pm.models.*;

import java.util.List;

/**
 * User: Oleg
 * Date: 15.01.2011
 */
public class ReserveAccountPage extends BasePage {
    @SpringBean
    private DealerShipManager dealerShipManager;

    @SpringBean
    private AccountManager accountManager;

    @SpringBean
    private ItemManager itemManager;

    public ReserveAccountPage() {
        this(new DealerShip());
    }

    public ReserveAccountPage(DealerShip dealerShip) {
        this(new DealerShipModel(dealerShip));
    }

    public ReserveAccountPage(DealerShipModel dealerShipModel) {
        add(new DealerShipForm("accountForm", dealerShipModel));
    }

    private class DealerShipForm extends Form<DealerShip> {

        public DealerShipForm(String id, IModel<DealerShip> dealerShipIModel) {
            super(id, new CompoundPropertyModel<DealerShip>(dealerShipIModel));

            // account drop down
            List<Account> accountsList = getAccountsList();
            DropDownChoice<Account> accountFromField = new DropDownChoice<Account> ("accountFrom", accountsList,
                    new ChoiceRenderer<Account>() {
                        @Override
                        public Object getDisplayValue(Account object) {
                            return object.getTitle();
                        }
                        @Override
                        public String getIdValue(Account object, int index) {
                            return Integer.toString(index);
                        }
                    });
            add(accountFromField);

            // description field
            TextField<String> descriptionField = new TextField<String> ("description");
            descriptionField.setRequired(true);
            add(descriptionField);

            // money field
            TextField<Long> moneyField = new TextField<Long>("money");
            moneyField.setRequired(true);
            add(moneyField);

            //feedback panel
            add(new FeedbackPanel("accountFeedback"/*, new ComponentFeedbackMessageFilter(this)*/));

            //cancel button
            Button cancelButton = new Button("cancelButton") {
                @Override
                public void onSubmit() {
                    setRedirect(true);
                    setResponsePage(MoneyTransferListPage.class);
                }
            };
            cancelButton.setDefaultFormProcessing(false);
            add(cancelButton);
        }

        @Override
        protected void onSubmit() {
            DealerShip dealerShip = this.getModelObject();
            User user = PMSession.get().getAuthenticatedUser();

            if (dealerShip.getAccountFrom() == null) {
                error("Please select account");
                return;
            }

            try {
                dealerShipManager.reserveMoney(dealerShip, user);
                setRedirect(true);
                setResponsePage(AccountListPage.class);
            } catch (IllegalAmountOfMoney illegalAmountOfMoney) {
                error("Not enough money for this");
            }
        }
    }

    List<Account> getAccountsList() {
        User user = PMSession.get().getAuthenticatedUser();
        return accountManager.getAccountsForUser(user);
    }
}
